<template>
  <div class="create-order">
    <Header title="生成订单"></Header>
    <van-contact-card type="edit" :tel="tel" :name="name" @click="onEdit" />
    <div class="content">
      <div v-for="(item, index) in cartStore.orderList" :key="index">
        <van-card
          :num="item.num"
          :price="item.price"
          :title="item.title"
          :thumb="item.pic"
        />
      </div>
    </div>
    <div class="pay-wrap">
      <div class="price">
        <span>商品金额</span>
        <span>￥{{ totalPrice }}</span>
      </div>
      <van-button
        type="primary"
        class="pay-btn"
        block
        color="#ffc400"
        @click="handleCreateOrder"
        >生成订单</van-button
      >
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import Header from "../home/components/Header.vue";
import { useCartStore } from "../../store/CartSore";
import { useRoute, useRouter } from "vue-router";
const tel = ref("18340918034");
const name = ref("lisi");
const totalPrice = ref(0);
const router = useRouter();
const route = useRoute();
const cartStore = useCartStore();
const onEdit = () => {
  router.push("/address")
};
const initPrice = () => {
  let price = 0;
  if (cartStore.orderList.length) {
    cartStore.orderList.forEach((item) => (price += item.num * item.price));
  }
  totalPrice.value = price;
};
const initAddres = () => {
  cartStore.userAddress.forEach((item) => {
    if (item.isDefault) {
      name.value = item.name;
      tel.value = item.tel;
    }
  });
};
const handleCreateOrder = () => {
  showDialog({
    title: "提示",
    message: "生成订单成功",
  }).then(() => {
    console.log(route.query.cartList);
    // on close
    let newCartlist = cartStore.cartList.filter((item) => {
      return !route.query.cartList.includes(item.id+"");
    });

    cartStore.deleteCart(newCartlist);
    cartStore.DoAppendorderList()
    router.push("/order");
  });
};
onMounted(() => {
  initPrice();
  initAddres();
});
</script>

<style scoped lang="less">
.create-order {
  display: flex;
  flex-flow: column;
  height: 100%;
  .content {
    flex: 1;
    overflow-y: auto;
  }
  .pay-wrap {
    padding: 10px 0;
    width: 100%;
    background: #fff;
    border-top: 1px solid #e9e9e9;
    > div {
      display: flex;
      justify-content: space-between;
      padding: 0 5%;
      margin: 10px 0;
      font-size: 30px;
      span:nth-child(2) {
        color: red;
        font-size: 30px;
      }
    }
    .pay-btn {
      width: 90%;
      margin: 0 auto;
    }
  }
}
</style>
